package com.aim.Activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.aim.Service.UploadPicService;
import com.aim.api.AIMCaseConstants;
import com.aim.util.Preferences;
import com.aim.util.WebServiceUtil;

public class Login extends Activity implements OnClickListener{
	Context mC;
	EditText userName;
	EditText passWord;
	Button buttonOK;
	ProgressDialog progressDialog;
	String usnm, pswd, imei;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	mC = this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        userName = (EditText) findViewById(R.id.editText1);
        passWord = (EditText) findViewById(R.id.editText2);
        buttonOK = (Button) findViewById(R.id.button1);
        buttonOK.setOnClickListener(this);
        
        
        
        WebServiceUtil.setContext(this);
        
    }
    
    public void onClick(View v) {
    	if (v.equals(buttonOK)) {
    		usnm = userName.getText().toString();
    		pswd = passWord.getText().toString();
    		TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    		
    		WifiManager wifimanager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
    		String macAddress= wifimanager.getConnectionInfo().getMacAddress();
    		
    		
//    		imei = telephonyManager.getDeviceId();
    		imei = "123";

    		loginProgress();
    	
    		
    	}

    }
    
    public void finish(){
    	mC = null;
    	userName = null;
    	passWord = null;
    	buttonOK = null;
    	progressDialog = null;
    	usnm = null;
    	pswd = null;
    	imei = null;
    	super.finish();
    }
    private void loginProgress(){
//    	flagAll = true;

        progressDialog = new ProgressDialog(mC);
        progressDialog.setButton("cancel", new DialogInterface.OnClickListener() {
        	 public void onClick(DialogInterface dialog, int i) {
//        		 flagAll = false;
        	  dialog.cancel();
        	 }
        	});
        progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        	 public void onCancel(DialogInterface dialog) {

        	  progressDialog.dismiss();
        	  
        	 }
        	});
    	progressDialog.setCancelable(true);
    	progressDialog.setMessage("Checking login information, please wait!");

    	progressDialog.show();

    	new Thread(){  
  		  
            @Override  
            public void run() {  
            	
            	Boolean b = WebServiceUtil.login("GetUser",usnm,pswd,imei);
        		
        		
        		
        		if(b){
        			
        			handler.sendEmptyMessage(0);
        			
        		}else{

        			handler.sendEmptyMessage(1);
        			}
            	  
                
            }}.start();
    	
    	
    	
    	
    }
    
    private Handler handler = new Handler(){  
  	  
        @Override  
        public void handleMessage(Message msg) {  
              
            if(msg.what ==0)
            {
            	progressDialog.dismiss();  
            	SharedPreferences sp = mC.getSharedPreferences(Preferences.NAME, 0);
    			Editor sharedata = sp.edit();
    			
    			sharedata.putString(AIMCaseConstants.USERNAME,usnm);
    			sharedata.putString(AIMCaseConstants.PASSWORD,pswd);
    			sharedata.putString(AIMCaseConstants.IMEI,imei);
    			sharedata.commit();
    			
    			
    			Intent mainMenu = new Intent(
						Login.this,
						Main.class);
    			
				startActivity(mainMenu);
            	
            }
            else if(msg.what ==1)
            {
	            	progressDialog.dismiss(); 
	            	Toast.makeText(mC, "Wrong Login Infomation, plz contact us", Toast.LENGTH_LONG).show();
        			
            }
	          
        }
        };
	
}